//
//  mm_preViewPicturesViewController.m
//  HondaPro_2
//
//  Created by Eric on 13-7-5.
//  Copyright (c) 2013年 glendale. All rights reserved.
//

#import "mm_preViewPicturesViewController.h"

@interface mm_preViewPicturesViewController () <mm_SLCoverFlowViewDataSource>
{
    UISlider *_widthSlider;
    UISlider *_heightSlider;
    UISlider *_spaceSlider;
    UISlider *_angleSlider;
    UISlider *_scaleSlider;
    
    CGFloat mm_SLCoverViewWidth;
    CGFloat mm_SLCoverViewHeight;
    CGFloat mm_SLCoverViewSpace;
    CGFloat mm_SLCoverViewAngle;
    CGFloat mm_SLCoverViewScale;
}

@end

@implementation mm_preViewPicturesViewController
@synthesize nPicNums = _nPicNums;
@synthesize nLimitOfAuthority = _nLimitOfAuthority;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        //添加标题
        [self createTitle];
        
//        //从本地将图片文件读入 业务模型 的 数组中
//        mm_picRecodeDataModel *modelIns = [mm_picRecodeDataModel sharedModelIns];
//        
//        if (0 == [modelIns.arrForAllCapturePic count])
//        {
//            NSFileManager *fileManager = [NSFileManager defaultManager];
//            
//            NSArray *myDocumentPaths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//            NSString *myDocumentPath = [myDocumentPaths objectAtIndex:0];
//            NSString *fileFolderPath = [myDocumentPath stringByAppendingPathComponent:@"mm_capturePicShow"];
//            NSLog(@"fileFolderPath = %@",fileFolderPath);
//            
//            NSArray *arrAllFiles = [fileManager contentsOfDirectoryAtPath:fileFolderPath error:nil];
//            NSLog(@"%@",arrAllFiles);
//            NSLog(@"%i",[arrAllFiles count]);
//            
//            for (int i = 0; i<[arrAllFiles count]; i++)
//            {
//                //读取出来每一个图片文件存入业务模型的数组中
//                NSString *strImg = [NSString stringWithFormat:@"img%i",i];
//                NSString *newFileName = [fileFolderPath stringByAppendingPathComponent:strImg];
//                NSData *dat = [fileManager contentsAtPath:newFileName];
//                UIImage *aimage = [UIImage imageWithData: dat];
//                UIImageView *imgView = [[UIImageView alloc] initWithImage:aimage];
//                [modelIns.arrForAllCapturePic setObject:imgView atIndexedSubscript:i];
//            }
//        }
    }
    return self;
}

#pragma mark - 初始化设置

-(void)setUpInit
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    CGRect frame = CGRectMake(50, 120, self.view.frame.size.width-100, self.view.frame.size.height-120);
    frame.size.height -= 50;
    _coverFlowView = [[mm_SLCoverFlowView alloc] initWithFrame:frame];
    _coverFlowView.backgroundColor = [UIColor whiteColor];
    _coverFlowView.delegate = self;
    _coverFlowView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    _coverFlowView.coverSize = CGSizeMake(mm_SLCoverViewWidth, mm_SLCoverViewHeight);
    _coverFlowView.coverSpace = 0.0;
    _coverFlowView.coverAngle = 0.0;
    _coverFlowView.coverScale = 1.0;
    [self.view addSubview:_coverFlowView];
    
    //给六宫格的图片设定参数
    
    mm_SLCoverViewWidth = _coverFlowView.frame.size.width-200;
    mm_SLCoverViewHeight = _coverFlowView.frame.size.height-100;
    mm_SLCoverViewSpace = 150;
    mm_SLCoverViewAngle = 0;
    mm_SLCoverViewScale = 1.0;
    
    // width
    _coverFlowView.coverSize = CGSizeMake(mm_SLCoverViewWidth * 1,
                                          _coverFlowView.coverSize.height);
    
    // height
    _coverFlowView.coverSize = CGSizeMake(_coverFlowView.coverSize.width,
                                          mm_SLCoverViewHeight * 1);
    
    // space
    _coverFlowView.coverSpace = 1 * mm_SLCoverViewSpace;
    
    // angle
    _coverFlowView.coverAngle = 1 * mm_SLCoverViewAngle;
    
    // scale
    _coverFlowView.coverScale = 1 * mm_SLCoverViewScale;
}

#pragma mark - 创建标题

-(void)createTitle
{
    UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(150, 20, 120, 50)];
    lbl.textColor = [HelpClass colorWithHexString:@"666666"];
    lbl.font = [UIFont systemFontOfSize:30];
    lbl.text = @"图片预览";
    [self.view addSubview:lbl];
}

- (void)loadView {
    [super loadView];
    
//    [self setUpInit];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self updateAllThePicScan];
    
    [_coverFlowView reloadData];
    
    //注册消息
    [self addAllObserver];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    //移除所有的消息
    [self removeAllObserver];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)valueChanged:(id)sender {
    UISlider *slider = (UISlider *)sender;
    if ([slider isEqual:_widthSlider]) {
        _coverFlowView.coverSize = CGSizeMake(mm_SLCoverViewWidth * _widthSlider.value,
                                              _coverFlowView.coverSize.height);
    } else if ([slider isEqual:_heightSlider]) {
        _coverFlowView.coverSize = CGSizeMake(_coverFlowView.coverSize.width,
                                              mm_SLCoverViewHeight * _heightSlider.value);
    } else if ([slider isEqual:_spaceSlider]) {
        _coverFlowView.coverSpace = _spaceSlider.value * mm_SLCoverViewSpace;
    } else if ([slider isEqual:_angleSlider]) {
        _coverFlowView.coverAngle = _angleSlider.value * mm_SLCoverViewAngle;
    } else if ([slider isEqual:_scaleSlider]) {
        _coverFlowView.coverScale = _scaleSlider.value * mm_SLCoverViewScale;
    }
}

#pragma mark - mm_mm_SLCoverFlowViewDataSource

//返回值为该界面中有几个可以滑动的视图
- (NSInteger)numberOfCovers:(mm_SLCoverFlowView *)coverFlowView
{
    int returnVal = _nPicNums / 6 + 1;
    if (0 == _nPicNums%6)
    {
        returnVal--;
    }
    if (1 == returnVal)
    {
        returnVal = 2;
    }
    return returnVal;
}

//此处添加View
- (mm_SLCoverView *)coverFlowView:(mm_SLCoverFlowView *)coverFlowView coverViewAtIndex:(NSInteger)index {
    mm_SLCoverView *view = [[mm_SLCoverView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 100.0)];
    
    view.backgroundColor = [UIColor whiteColor];
    
    /*给View上添加六宫格*/
    NSDictionary *dicForRect = [NSDictionary dictionaryWithObjectsAndKeys:
                                [NSValue valueWithCGRect:CGRectMake(0+2, 0+50, 220, 165)],@"page1",
                                [NSValue valueWithCGRect:CGRectMake(250+2, 0+50, 220, 165)],@"page2",
                                [NSValue valueWithCGRect:CGRectMake(500+2, 0+50, 220, 165)],@"page3",
                                [NSValue valueWithCGRect:CGRectMake(0+2, 195+50, 220, 165)],@"page4",
                                [NSValue valueWithCGRect:CGRectMake(250+2, 195+50, 220, 165)],@"page5",
                                [NSValue valueWithCGRect:CGRectMake(500+2, 195+50, 220, 165)],@"page6", nil];
    
    mm_picRecodeDataModel *modelIns = [mm_picRecodeDataModel sharedModelIns];
    
    //贴预览图的个数为：nPicDis
    int nPicDis = (_nPicNums-6*index) >= 6 ? (6) : (_nPicNums-6*index);
    for (int i = 0; i<nPicDis; i++)
    {
        NSString *str = [NSString stringWithFormat:@"page%i",i+1];
        CGRect rect = (CGRect)[[dicForRect objectForKey:str] CGRectValue];
        UIImageView *imgView = [modelIns.arrForAllCapturePic objectAtIndex:(6*index)+i];
        
        imgView.frame = rect;
        
        [view addSubview:imgView];
        
        //给所有的 imageView 添加手势
        UIGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SelectTap:)];
        recognizer.delegate =self;
        [imgView setUserInteractionEnabled:YES];//这句很重要特别是imgView
        [imgView addGestureRecognizer:recognizer];
        
        //给图片的下边添加一个label，显示标注点和备注
        if ([self imgViewsIndexInPicModelByImgView:imgView] < [[MainMenuDataModel sharedMainMenuDataModel].arrImageList count])
        {
            continue;
        }
        
        CGFloat _xVal = imgView.frame.origin.x;
        CGFloat _yVal = imgView.frame.origin.y;
        CGFloat _widthVal = imgView.frame.size.width;
        CGFloat _heightVal = imgView.frame.size.height;
        
        NSString *strFaultNameTmp = [self returnFaultyNameFromOutCheckListByImageIndex:[self imgViewsIndexInPicModelByImgView:imgView]-[[MainMenuDataModel sharedMainMenuDataModel].arrImageList count]];
        NSString *strFaultyRemarkTmp = [self returnFaultyRemarkFromOutCheckListByImageIndex:[self imgViewsIndexInPicModelByImgView:imgView]-[[MainMenuDataModel sharedMainMenuDataModel].arrImageList count]];
        
        UILabel *lblTmp = [[UILabel alloc] initWithFrame:CGRectMake(_xVal, _yVal+_heightVal, _widthVal, 30)];
        lblTmp.backgroundColor = [UIColor colorWithRed:0.98 green:0.98 blue:0.98 alpha:1];
        lblTmp.text = [strFaultNameTmp stringByAppendingFormat:@" 备注:%@",strFaultyRemarkTmp];
        lblTmp.textColor = [UIColor darkGrayColor];
        lblTmp.font = [UIFont systemFontOfSize:13];
        lblTmp.numberOfLines = 10;
        
        [view addSubview:lblTmp];
    }
    /*end of 给View上添加六宫格*/
    
    return view;
}

#pragma mark - imageView添加的手势回调方法

-(void)SelectTap:(UITapGestureRecognizer*)recognizer
{
    NSLog(@"selected !!!");
    
    /*
     此处的逻辑：
     （1）如果是查看模式，就调用查看模式的代码，return；
     （2）如果是编辑模式，就设置要删除的图片的下标，并且重新设置按钮的状态
     */
    
    UIView *view = recognizer.view;
    UIImageView *imgView = (UIImageView *)view;
    
    //（1）如果是查看模式，就调用查看模式的代码，return；
    if (MM_PIC_SCAN_MODE_ == _picMode)
    {
        static BOOL bFlag = NO;
        
        if (NO == bFlag)
        {
            imgView.frame = self.view.frame;
            [self.view addSubview:imgView];
            bFlag = YES;
        }
        else
        {
            [imgView removeFromSuperview];
            
            [self setUpInit];
            
            [_coverFlowView reloadData];
            bFlag = NO;
        }
        
        return;
    }
    
    //（2）如果是编辑模式，就设置要删除的图片的下标，并且重新设置按钮的状态
    if (MM_PIC_EDIT_MODE_ == _picMode)
    {
        _picIndexToDel = [self imgViewsIndexInPicModelByImgView:imgView];
        
        int i = 0;
        if ((i = [self picIndexInDelArrayByReference:_picIndexToDel]) != -1)
        {
            //选中的索引已经被选择过，将该索引移除掉
            [_arrAllPicIndexToDel removeObjectAtIndex:i];
        }
        else
        {
            //将要删除的索引存入数组中
            [_arrAllPicIndexToDel addObject:[NSNumber numberWithInteger:_picIndexToDel]];
        }
        
        [HelpClass setUpTheStateButtonEnableState:MM_PIC_BTN_DISABLED_ withBtn:self.editBtnIns];
        [HelpClass setUpTheStateButtonEnableState:MM_PIC_BTN_ENABLED_ withBtn:self.deleteBtnIns];
        [HelpClass setUpTheStateButtonEnableState:MM_PIC_BTN_ENABLED_ withBtn:self.cancelBtnIns];
        
        if ([_arrAllPicIndexToDel count] == 0)
        {
            [HelpClass setUpTheStateButtonEnableState:MM_PIC_BTN_DISABLED_ withBtn:self.deleteBtnIns];
        }
        
        //重新标注图片的图标
        [self remarkAllThePicSmallIcon];
    }
}

#pragma mark - Private methods

- (UISlider *)addSliderWithMinY:(CGFloat)minY labelText:(NSString *)labelText {
    CGRect labelFrame = CGRectMake(20.0, minY, 80.0, 30.0);
    UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];
    label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin;
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont systemFontOfSize:22.0];
    label.text = labelText;
    label.textColor = [UIColor darkTextColor];
    [self.view addSubview:label];
    
    CGRect sliderFrame = CGRectMake(CGRectGetMaxX(labelFrame) + 20.0, minY, 200.0, 30.0);
    sliderFrame.size.width = CGRectGetWidth(self.view.bounds) - CGRectGetMaxX(labelFrame) - 40.0;
    UISlider *slider = [[UISlider alloc] initWithFrame:sliderFrame];
    slider.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin;
    slider.minimumValue = 0.0;
    slider.maximumValue = 2.0;
    slider.value = 1.0;
    [slider addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:slider];
    return slider;
}

#pragma mark - button 的回调方法

- (IBAction)backBtnPressed:(UIButton *)sender
{
    NSLog(@"backBtnPressed !!!");
    
    //跳回上一级页面
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)homeBtnPressed:(UIButton *)sender
{
    NSLog(@"homeBtnPressed !!!");
    
    //跳回主界面
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (IBAction)editBtnPressed:(UIButton *)sender
{
    /*
     （1）设置编辑按钮的状态为禁能状态、删除按钮的状态为禁能状态、取消按钮状态为使能状态；
     （2）设置图片的预览模式为编辑模式
     */
    
    //（1）设置编辑按钮的状态为禁能状态、删除按钮的状态为禁能状态、取消按钮状态为使能状态；
    [HelpClass setUpTheStateButtonEnableState:MM_PIC_BTN_DISABLED_ withBtn:self.editBtnIns];
    [HelpClass setUpTheStateButtonEnableState:MM_PIC_BTN_DISABLED_ withBtn:self.deleteBtnIns];
    [HelpClass setUpTheStateButtonEnableState:MM_PIC_BTN_ENABLED_ withBtn:self.cancelBtnIns];
    
    //（2）设置图片的预览模式为编辑模式
    _picMode = MM_PIC_EDIT_MODE_;
}

- (IBAction)deleteBtnPressed:(UIButton *)sender
{
    /*
     （1）调用删除图片的接口,DeleteOutCheckPic.aspx?picName=20130901132859&operatorID=2001
     */
    
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    
    _nCountPicToDel = [_arrAllPicIndexToDel count];
    
    if (_nCountPicToDel == 0)
    {
        UIAlertView *alerVeiw = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"请选择要删除的图片！！！" delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];
        [alerVeiw show];
        
        return;
    }
    
    [SVProgressHUD showWithStatus:@"正在删除" maskType:SVProgressHUDMaskTypeGradient];
    
    _picIndexToDel = [[_arrAllPicIndexToDel objectAtIndex:[_arrAllPicIndexToDel count]-_nCountPicToDel] integerValue];
    
    //图片名称
    modelIns.strCurrentPicName = [self picNameByIndex:_picIndexToDel];
        
    //接口请求
    NSMutableArray *arrTmp = [NSMutableArray array];
    [arrTmp addObject:@"picName"];
    [arrTmp addObject:[self picPureNameByOriginalName:modelIns.strCurrentPicName]];
    
    [arrTmp addObject:@"operatorID"];
    [arrTmp addObject:modelIns.strCurrentSalerId];
    
    [HelpClass getWebServiceDataByString:MM_DELETE_OUT_CHECK_PIC_ andArrObject:arrTmp];
    
    _nCountPicToDel--;
}

- (IBAction)cancelBtnPressed:(UIButton *)sender
{
    /*
     （1）更新图片预览界面；
     */
    
    //（1）更新图片预览界面
    [self updateAllThePicScan];
}

- (void)viewDidUnload
{
    [self setEditBtnIns:nil];
    [self setDeleteBtnIns:nil];
    [self setCancelBtnIns:nil];
    [super viewDidUnload];
}

//调用图片删除接口的回调方法
-(void)getDeleteServerMsg:(NSNotification *)notification
{
    NSDictionary *theJson = [NSJSONSerialization JSONObjectWithData:(NSData*)notification.object options:NSJSONReadingMutableLeaves error:Nil];
    NSLog(@"%@",theJson);
    
    /*
     {"error_code":-1,"error_msg":"DMS提示信息：不存在该图片！！"}
     */
    
    /*
     （1）如果删除成功，提示删除成功；否则提示删除失败，return；
     （2）更新作业单里边图片列表；
     （3）更新图片预览界面
     */
    
//    //（1）如果删除成功，提示删除成功；否则提示删除失败，return；
//    if ([[theJson objectForKey:@"error_code"] integerValue] != 0)
//    {
//        UIAlertView *alerVeiw = [[UIAlertView alloc] initWithTitle:@"删除失败" message:[theJson objectForKey:@"error_msg"] delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];
//        [alerVeiw show];
//        
//        return;
//    }
//    else
//    {
//        UIAlertView *alerVeiw = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"删除成功！！！" delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];
//        [alerVeiw show];
//    }
    
    if (_nCountPicToDel == 0)
    {
        UIAlertView *alerVeiw = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"删除成功！！！" delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];
        [alerVeiw show];
        
        //给 _arrAllPicIndexToDel 数组排序(从小到大)
        _arrAllPicIndexToDel = [NSMutableArray arrayWithArray:_arrAllPicIndexToDel];
        for (int i = 0; i<[_arrAllPicIndexToDel count]-1; i++)
        {
            for (int j = 0; j<[_arrAllPicIndexToDel count]-1-i; j++)
            {
                NSInteger nDelTmp_first = [[_arrAllPicIndexToDel objectAtIndex:j] integerValue];
                NSInteger nDelTmp_second = [[_arrAllPicIndexToDel objectAtIndex:j+1] integerValue];
                if (nDelTmp_first > nDelTmp_second)
                {
                    //交换位置
                    [_arrAllPicIndexToDel setObject:[NSNumber numberWithInteger:nDelTmp_second] atIndexedSubscript:j];
                    [_arrAllPicIndexToDel setObject:[NSNumber numberWithInteger:nDelTmp_first] atIndexedSubscript:j+1];
                }
            }
        }

        for (int i = 0; i<[_arrAllPicIndexToDel count]; i++)
        {
            _picIndexToDel = [[_arrAllPicIndexToDel objectAtIndex:i] integerValue];

            NSInteger nCountdelTmp = 0;
            for (int j = 0; j<[_arrAllPicIndexToDel count]; j++)
            {
                if (_picIndexToDel == [[_arrAllPicIndexToDel objectAtIndex:j] integerValue])
                {
                    nCountdelTmp = j+1-1;
                    break;
                }
            }
            
            //（2）更新作业单里边图片列表；
            MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
            if (_picIndexToDel < [modelIns.arrImageList count])
            {
                modelIns.arrImageList = [NSMutableArray arrayWithArray:modelIns.arrImageList];
                [modelIns.arrImageList removeObjectAtIndex:_picIndexToDel-nCountdelTmp];
            }
            else
            {
                //删除故障点对应的图片
                [self deleteTheImageFromOutCheckListByImageIndex:_picIndexToDel-[modelIns.arrImageList count]-nCountdelTmp];
            }
            
            mm_picRecodeDataModel *picModelIns = [mm_picRecodeDataModel sharedModelIns];
            picModelIns.arrForAllCapturePic = [NSMutableArray arrayWithArray:picModelIns.arrForAllCapturePic];
            [picModelIns.arrForAllCapturePic removeObjectAtIndex:_picIndexToDel-nCountdelTmp];
        }
        
//        for (int i = 0; i<[_arrAllPicIndexToDel count]; i++)
//        {
//            _picIndexToDel = [[_arrAllPicIndexToDel objectAtIndex:i] integerValue];
//            //（2）更新作业单里边图片列表；
//            MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
//            if (_picIndexToDel < [modelIns.arrImageList count])
//            {
//                modelIns.arrImageList = [NSMutableArray arrayWithArray:modelIns.arrImageList];
//                [modelIns.arrImageList removeObjectAtIndex:_picIndexToDel];
//            }
//            else
//            {
//                //删除故障点对应的图片
//                [self deleteTheImageFromOutCheckListByImageIndex:_picIndexToDel-[modelIns.arrImageList count]];
//            }
//            
//            mm_picRecodeDataModel *picModelIns = [mm_picRecodeDataModel sharedModelIns];
//            picModelIns.arrForAllCapturePic = [NSMutableArray arrayWithArray:picModelIns.arrForAllCapturePic];
//            [picModelIns.arrForAllCapturePic removeObjectAtIndex:_picIndexToDel];
//        }
        
        //（3）更新图片预览界面
        [self updateAllThePicScan];
    }
    else
    {
        MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
        
        _picIndexToDel = [[_arrAllPicIndexToDel objectAtIndex:[_arrAllPicIndexToDel count]-_nCountPicToDel] integerValue];
        
        //图片名称
        modelIns.strCurrentPicName = [self picNameByIndex:_picIndexToDel];
        
        //接口请求
        NSMutableArray *arrTmp = [NSMutableArray array];
        [arrTmp addObject:@"picName"];
        [arrTmp addObject:[self picPureNameByOriginalName:modelIns.strCurrentPicName]];
        
        [arrTmp addObject:@"operatorID"];
        [arrTmp addObject:modelIns.strCurrentSalerId];
        
        [HelpClass getWebServiceDataByString:MM_DELETE_OUT_CHECK_PIC_ andArrObject:arrTmp];
        
        _nCountPicToDel--;
    }
    
    [SVProgressHUD dismiss];
}

//注册消息
-(void)addAllObserver
{
    //注册图片删除的消息
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getDeleteServerMsg:) name:MM_DELETE_OUT_CHECK_PIC_ object:nil];
}

//移除消息
-(void)removeAllObserver
{
    //移除本类中的消息
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

//设置整个界面数据
-(void)setUpAllThePicScanData
{
    self.nPicNums = [[mm_picRecodeDataModel sharedModelIns].arrForAllCapturePic count];
    
    [_coverFlowView removeFromSuperview];
    [self setUpInit];
    
    [_coverFlowView reloadData];
    
    //当前的模式－－－查看、编辑
    _picMode = MM_PIC_SCAN_MODE_;
    //当前要删除的下标
    _picIndexToDel = NSNotFound;
    
    _arrAllPicIndexToDel = [NSMutableArray array];
}

//更新图片预览界面
-(void)updateAllThePicScan;
{
    /*
     （1）如果图片界面权限为查看，设置编辑按钮的状态为禁能状态、删除按钮的状态为禁能状态、取消按钮状态为禁能状态；
     （2）如果图片界面模式为编辑，设置编辑按钮的状态为使能状态、删除按钮的状态为禁能状态、取消按钮状态为禁能状态；
     （3）重新设置界面的数据
     */
    
    //（1）如果图片界面权限为查看，设置编辑按钮的状态为禁能状态、删除按钮的状态为禁能状态、取消按钮状态为禁能状态；
    //（2）如果图片界面权限为编辑，设置编辑按钮的状态为使能状态、删除按钮的状态为禁能状态、取消按钮状态为禁能状态；
    if (MM_PIC_CAN_NOT_UPDATE_ == self.nLimitOfAuthority)
    {
        [HelpClass setUpTheStateButtonEnableState:MM_PIC_BTN_DISABLED_ withBtn:self.editBtnIns];
        [HelpClass setUpTheStateButtonEnableState:MM_PIC_BTN_DISABLED_ withBtn:self.deleteBtnIns];
        [HelpClass setUpTheStateButtonEnableState:MM_PIC_BTN_DISABLED_ withBtn:self.cancelBtnIns];
    }
    else if (MM_PIC_CAN_UPDATE_ == self.nLimitOfAuthority)
    {
        [HelpClass setUpTheStateButtonEnableState:MM_PIC_BTN_ENABLED_ withBtn:self.editBtnIns];
        [HelpClass setUpTheStateButtonEnableState:MM_PIC_BTN_DISABLED_ withBtn:self.deleteBtnIns];
        [HelpClass setUpTheStateButtonEnableState:MM_PIC_BTN_DISABLED_ withBtn:self.cancelBtnIns];
    }
    
    //移除imgView上的所有子视图
    mm_picRecodeDataModel *modelIns = [mm_picRecodeDataModel sharedModelIns];
    
    for (int i = 0; i<[modelIns.arrForAllCapturePic count]; i++)
    {
        UIImageView *imgViewTmp = modelIns.arrForAllCapturePic[i];
        for (id obj in [imgViewTmp subviews])
        {
            [obj removeFromSuperview];
        }
    }
    
    //重新给imgView添加图标（主要是给故障点对应的照片添加图标）
    for (int i = 0; i<[modelIns.arrForAllCapturePic count]; i++)
    {
        if (i<[[MainMenuDataModel sharedMainMenuDataModel].arrImageList count])
        {
            continue;
        }
        UIImageView *imgViewToAdd = modelIns.arrForAllCapturePic[i];
        UILabel *lblTmp = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
        lblTmp.backgroundColor = [UIColor clearColor];
        lblTmp.text = [self returnFaultyNameFromOutCheckListByImageIndex:i-[[MainMenuDataModel sharedMainMenuDataModel].arrImageList count]];
        lblTmp.textColor = [UIColor darkGrayColor];
        lblTmp.font = [UIFont systemFontOfSize:15];
        [imgViewToAdd addSubview:lblTmp];
    }
    
    //（3）重新设置界面的数据
    [self setUpAllThePicScanData];
}

//通过本类中的UIimageView,找到其再界面中的编号
-(NSInteger)imgViewsIndexInPicModelByImgView:(UIImageView *)imgView
{    
    return [[mm_picRecodeDataModel sharedModelIns].arrForAllCapturePic indexOfObject:imgView];
}

//根据编号，由数据模型中取得图片名称,xxxx.jpg
-(NSString *)picNameByIndex:(NSInteger)index
{
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    
    if (index < [modelIns.arrImageList count])
    {
        return [[modelIns.arrImageList objectAtIndex:index] objectForKey:@"imageName"];
    }
    else
    {
        return [self returnImageNameFromOutCheckListByImageIndex:index-[modelIns.arrImageList count]];
    }
    
    return nil;
}

//获取图片名xxxx，参数为xxxx.jpg
-(NSString *)picPureNameByOriginalName:(NSString *)picName
{
    NSRange range = [picName rangeOfString:@"."];
    
    return [picName substringToIndex:range.location];
}

//将故障点中的所有图片拿出来组成一个行的列表
-(NSArray *)arrPicsInOutCheckList
{
    mm_picRecodeDataModel *picModelIns = [mm_picRecodeDataModel sharedModelIns];
    NSMutableArray *arrFinalList = [NSMutableArray array];
    
    for (int i = 0; i<[picModelIns.arrForAllFaultyObj count]; i++)
    {
        NSArray *arrImageListTmp = [[picModelIns.arrForAllFaultyObj objectAtIndex:i] objectForKey:MM_OUT_CHECK_IMAGE_NAME_LIST_KEY_STR_];
        for (int j = 0; j<[arrImageListTmp count]; j++)
        {
            [arrFinalList addObject:[arrImageListTmp objectAtIndex:j]];
        }
    }
    
    return arrFinalList;
}

//通过传入的索引 i（i为故障点中对应的照片索引） 返回故障点中图片的名字STR
-(NSString *)returnImageNameFromOutCheckListByImageIndex:(NSInteger)indexXingCan
{
    mm_picRecodeDataModel *picModelIns = [mm_picRecodeDataModel sharedModelIns];
    
    NSInteger nCount = 0;
    for (int i = 0; i<[picModelIns.arrForAllFaultyObj count]; i++)
    {
        NSArray *arrImageListTmp = [[picModelIns.arrForAllFaultyObj objectAtIndex:i] objectForKey:MM_OUT_CHECK_IMAGE_NAME_LIST_KEY_STR_];
        for (int j = 0; j<[arrImageListTmp count]; j++)
        {
            if (nCount == indexXingCan)
            {
                return [[[[picModelIns.arrForAllFaultyObj objectAtIndex:i] objectForKey:MM_OUT_CHECK_IMAGE_NAME_LIST_KEY_STR_] objectAtIndex:j] objectForKey:MM_OUT_CHECK_IMAGE_NAME_KEY_STR_];
            }
            nCount ++;
        }
    }
    
    return @"";
}

//通过传入的索引 i 返回故障点的标记编号（名称）
-(NSString *)returnFaultyNameFromOutCheckListByImageIndex:(NSInteger)indexXingCan
{
    /*
     NSMutableDictionary *dicTmp = [NSMutableDictionary dictionaryWithObjectsAndKeys:
     [NSValue valueWithCGPoint:self.point],@"point",
     self.idIndexStr,@"idIndexStr",
     self.typeStr,@"typeStr",
     self.numberStr,@"numberStr",
     self.remarksStr,@"remarksStr",
     self.imgname,@"imgname",
     self.imgView,@"imgView", nil];
     */
    mm_picRecodeDataModel *picModelIns = [mm_picRecodeDataModel sharedModelIns];
    
    NSInteger nCount = 0;
    for (int i = 0; i<[picModelIns.arrForAllFaultyObj count]; i++)
    {
        NSArray *arrImageListTmp = [[picModelIns.arrForAllFaultyObj objectAtIndex:i] objectForKey:MM_OUT_CHECK_IMAGE_NAME_LIST_KEY_STR_];
        for (int j = 0; j<[arrImageListTmp count]; j++)
        {
            if (nCount == indexXingCan)
            {
                return [[picModelIns.arrForAllFaultyObj objectAtIndex:i] objectForKey:@"idIndexStr"];
            }
            nCount ++;
        }
    }
    
    return @"";
}

//通过传入的索引 i 返回故障点的备注
-(NSString *)returnFaultyRemarkFromOutCheckListByImageIndex:(NSInteger)indexXingCan
{
    /*
     NSMutableDictionary *dicTmp = [NSMutableDictionary dictionaryWithObjectsAndKeys:
     [NSValue valueWithCGPoint:self.point],@"point",
     self.idIndexStr,@"idIndexStr",
     self.typeStr,@"typeStr",
     self.numberStr,@"numberStr",
     self.remarksStr,@"remarksStr",
     self.imgname,@"imgname",
     self.imgView,@"imgView", nil];
     */
    mm_picRecodeDataModel *picModelIns = [mm_picRecodeDataModel sharedModelIns];
    
    NSInteger nCount = 0;
    for (int i = 0; i<[picModelIns.arrForAllFaultyObj count]; i++)
    {
        NSArray *arrImageListTmp = [[picModelIns.arrForAllFaultyObj objectAtIndex:i] objectForKey:MM_OUT_CHECK_IMAGE_NAME_LIST_KEY_STR_];
        for (int j = 0; j<[arrImageListTmp count]; j++)
        {
            if (nCount == indexXingCan)
            {
                return [[picModelIns.arrForAllFaultyObj objectAtIndex:i] objectForKey:@"remarksStr"];
            }
            nCount ++;
        }
    }
    
    return @"";
}

//通过传入的索引 i 删除故障点对应的图片
-(BOOL)deleteTheImageFromOutCheckListByImageIndex:(NSInteger)indexXingCan
{
    mm_picRecodeDataModel *picModelIns = [mm_picRecodeDataModel sharedModelIns];
    picModelIns.arrForAllFaultyObj = [NSMutableArray arrayWithArray:picModelIns.arrForAllFaultyObj];
    
    NSInteger nCount = 0;
    for (int i = 0; i<[picModelIns.arrForAllFaultyObj count]; i++)
    {
        NSMutableArray *arrImageListTmp = [NSMutableArray arrayWithArray:[[picModelIns.arrForAllFaultyObj objectAtIndex:i] objectForKey:MM_OUT_CHECK_IMAGE_NAME_LIST_KEY_STR_]];
        for (int j = 0; j<[arrImageListTmp count]; j++)
        {
            if (nCount == indexXingCan)
            {
                //删除该图片
                [arrImageListTmp removeObjectAtIndex:j];
                NSMutableDictionary *dicTmp = [NSMutableDictionary dictionaryWithDictionary:[picModelIns.arrForAllFaultyObj objectAtIndex:i]];
                [dicTmp setObject:arrImageListTmp forKey:MM_OUT_CHECK_IMAGE_NAME_LIST_KEY_STR_];
                [picModelIns.arrForAllFaultyObj setObject:dicTmp atIndexedSubscript:i];
                
                return YES;
            }
            nCount ++;
        }
    }
    
    return NO;
}

//删除的项目是否在选中列表中，是的话返回数组的编号,否则返回-1
-(NSInteger)picIndexInDelArrayByReference:(NSInteger)_indexXingCan
{
    for (int i = 0; i<[_arrAllPicIndexToDel count]; i++)
    {
        NSInteger indexTmp = [[_arrAllPicIndexToDel objectAtIndex:i] integerValue];
        if (_indexXingCan == indexTmp)
        {
            return i;
        }
    }
    
    return -1;
}

//重新标注要删除的图片图标
-(void)remarkAllThePicSmallIcon
{
    //移除imgView上的所有子视图
    mm_picRecodeDataModel *modelIns = [mm_picRecodeDataModel sharedModelIns];
    for (int i = 0; i<[modelIns.arrForAllCapturePic count]; i++)
    {
        UIImageView *imgViewTmp = [modelIns.arrForAllCapturePic objectAtIndex:i];
        for (id obj in [imgViewTmp subviews])
        {
            [obj removeFromSuperview];
        }
    }
    
    //重新给imgView添加图标（主要是给故障点对应的照片添加图标）
    for (int i = 0; i<[modelIns.arrForAllCapturePic count]; i++)
    {
        if (i<[[MainMenuDataModel sharedMainMenuDataModel].arrImageList count])
        {
            continue;
        }
        UIImageView *imgViewToAdd = [modelIns.arrForAllCapturePic objectAtIndex:i];
        UILabel *lblTmp = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
        lblTmp.backgroundColor = [UIColor clearColor];
        lblTmp.text = [self returnFaultyNameFromOutCheckListByImageIndex:i-[[MainMenuDataModel sharedMainMenuDataModel].arrImageList count]];
        lblTmp.textColor = [UIColor darkGrayColor];
        lblTmp.font = [UIFont systemFontOfSize:15];
        [imgViewToAdd addSubview:lblTmp];
    }
    
    //（2）给选中的imgView的右下角添加一个小的对号图标
    for (int i = 0; i<[_arrAllPicIndexToDel count]; i++)
    {
        NSInteger indexTmp = [[_arrAllPicIndexToDel objectAtIndex:i] integerValue];
        UIImageView *imgView = [[mm_picRecodeDataModel sharedModelIns].arrForAllCapturePic objectAtIndex:indexTmp];
        UIImageView *imgViewTmp = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"对号.png"]];
        imgViewTmp.backgroundColor = [UIColor clearColor];
        imgViewTmp.frame = CGRectMake(imgView.frame.size.width-40, imgView.frame.size.height-40, 40, 40);
        [imgView addSubview:imgViewTmp];
    }
}

@end
